package com.zhongfl.crm.lixiao.batch.task;

import com.zhongfl.crm.lixiao.batch.service.OrderSyncFromBoss2LXService;
import com.zhongfl.crm.lixiao.mapper.OrderTaskLogJobMapper;
import com.zhongfl.crm.model.boss.OrderTaskLog;
import org.springframework.stereotype.Component;
import tech.heptagram.core.common.batch.task.TaskService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * BOSS订单信息同步到励销服务
 *
 * @author wang.yuchuan
 * @date 2018/6/28
 */
@Component
public class OrderSyncFromBoss2LXTask extends TaskService<OrderTaskLog> {

    @Resource
    private OrderTaskLogJobMapper jobMapper;

    @Resource
    private OrderSyncFromBoss2LXService orderSyncFromBoss2LXService;

    @Override
    public boolean lock(OrderTaskLog task) {
        if (Objects.isNull(task)) {
            return false;
        }
        if (Objects.isNull(task.getId())) {
            return false;
        }
        return jobMapper.lock(task.getId()) == DEFAULT_UPDATE_COUNT;
    }

    @Override
    public void exec(OrderTaskLog task) throws RuntimeException {
        orderSyncFromBoss2LXService.sync(task);
    }

    @Override
    public void success(OrderTaskLog task) throws RuntimeException {
        if (Objects.isNull(task)) {
            return;
        }
        jobMapper.success(task);
    }

    @Override
    public void fail(OrderTaskLog task, String failedMsg) throws RuntimeException {
        jobMapper.fail(task);
    }

    @Override
    public void reset(OrderTaskLog task) throws RuntimeException {
        jobMapper.reset(task);
    }

    @Override
    public int getTaskTimes(OrderTaskLog task) {
        return task.getDealTimes();
    }

    @Override
    public void setTaskTimes(OrderTaskLog task, Integer taskTimes) {
        task.setDealTimes(taskTimes);
    }

    @Override
    public List<OrderTaskLog> find() {
        return jobMapper.selectUnDealedTasks();
    }
}
